package com.mdata.config;

import com.mdata.common.DefinitionException;
import com.mdata.common.ResponseEnum;
import com.mdata.common.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 登录异常
     */
    @ExceptionHandler(value = {UnknownAccountException.class, IncorrectCredentialsException.class})
    public ResultData userRealmHandler(Exception exception) {
        log.error("登录异常 userRealmHandler：", exception);
        return ResultData.error(ResponseEnum.INSIDE_ERROR_SESSION_ARCHIVING_INIT);
    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = DefinitionException.class)
    public ResultData bizExceptionHandler(DefinitionException exception) {
        log.error("自定义异常 bizExceptionHandler：", exception);
        return ResultData.error(exception.getErrorCode(), exception.getErrorMsg());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    public ResultData exceptionHandler(Exception exception) {
        log.error("其他异常 exceptionHandler：", exception);
        return ResultData.error(ResponseEnum.INTERNAL_SERVER_ERROR);
    }
}
